package com.livedocumentation.client.webservices;

import java.util.ArrayList;

import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.json.client.JSONObject;
import com.google.gwt.jsonp.client.JsonpRequestBuilder;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.livedocumentation.client.GWTEntryPoint;
import com.livedocumentation.client.model.ProjectClass;
import com.livedocumentation.client.web.MainPage;
import com.livedocumentation.client.web.description.FeaturePanel;
import com.livedocumentation.client.web.menu.Menu;

public class RequestSubversionSpecificationFile {

	// http://livesourceagile.com:8080/LiveSourceWebServices/GetSubversionSpecificationFileService?
	// repositoryURL=https://tictactoe4j.googlecode.com/svn/trunk
	// &username=alline.oliveira&password=bD8Km8se2rV9
	// &projectID=tictactoe4j

	private static String url = "http://livesourceagile.com:8080/LiveSourceWebServices2/GetSubversionSpecificationFileService";

	public static void loadJSONFile(final String classPath) {

		// String username = EncryptText.decrypt(GetCookies.getUsername(project
		// .getProjectID()));
		String username = null;

		// String password =
		// EncryptText.decrypt(GetCookies.getPassword(project.getProjectID()));
		String password = null;

		JsonpRequestBuilder jsonp = new JsonpRequestBuilder();

		jsonp.setTimeout(50000);

		String urlSave = url + "?repositoryURL="
				+ GWTEntryPoint.currentProject.getRepositoryURL()
				+ "&projectID=" + GWTEntryPoint.currentProject.getProjectID()
				+ "&username=" + username + "&password=" + password;

		jsonp.requestObject(urlSave, new AsyncCallback<JavaScriptObject>() {

			public void onFailure(Throwable throwable) {

				System.out.println("Error: " + throwable);
				Window.alert(throwable.toString());
			}

			public void onSuccess(JavaScriptObject feed) {

				JSONObject objFeed = new JSONObject(feed);

				ArrayList<ProjectClass> sourceList = new ArrayList<ProjectClass>();

				ProjectClass projectClassSelected = null;

				for (int i = 0; i < objFeed.size(); i++) {

					JSONObject entry = (JSONObject) objFeed.get(i + "");

					ProjectClass projectClass = new ProjectClass();

					String fileExtension = FileExtensions
							.getFileExtension(ConvertJsonp
									.convertToString(entry.get("fileName")));

					if (entry.get("class_tags") != null
							&& FileExtensions.EXTENSION_JAVA
									.equals(fileExtension)) {

						projectClass
								.setFeatureType(ConvertJsonpToJavaDocumentation
										.getClassType(entry.get("class_tags")
												.isArray()));

						if (projectClass.getFeatureType() != null) {

							projectClass = convertJsonToClass(projectClass,
									entry);

							sourceList.add(projectClass);

							if (classPath != null
									&& classPath.equals(projectClass.toString())) {

								projectClassSelected = projectClass;
							}
						}
					}
				}

				GWTEntryPoint.sourceFeatures = sourceList;

				MainPage.documentationMenu.add(new Menu());

				if (projectClassSelected != null) {

					MainPage.documentationPanel.clear();

					MainPage.documentationPanel.add(new FeaturePanel(
							projectClassSelected));
				}
			}
		});
	}

	private static ProjectClass convertJsonToClass(ProjectClass projectClass,
			JSONObject entry) {

		projectClass = ConvertJsonpToJavaDocumentation.convert(projectClass,
				entry);

		projectClass.setStatus(ConvertJsonpToJavaDocumentation.getStatus(entry
				.get("class_tags").isArray()));

		projectClass.setFilePath(ConvertJsonp.convertToString(entry
				.get("filePath")));

		projectClass.setFileName(ConvertJsonp.convertToString(entry
				.get("fileName")));

		projectClass.setFeaturePath(FeatureNameGenerator.getLastNameSpaced(
				projectClass.getFeaturePath(), "/"));

		projectClass.setFeatureName(FeatureNameGenerator
				.getFileSpacedName(projectClass.getFileName()));

		return projectClass;
	}
}
